package com.diy.sigmund.diycommon.spring.extensionpoint;

import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * <br>
 * <br>Line 2958: 2022-09-20 16:56:58.299 [diy-common] [172.25.96.1] [INFO ] [main] [com.diy.sigmund.diycommon.spring.extensionpoint.CustomInstantiationAwareBeanPostProcessor] - beanClass = class org.springframework.jdbc.datasource.DataSourceTransactionManager, beanName = transactionManager
 * <br>Line 2974: 2022-09-20 16:56:58.322 [diy-common] [172.25.96.1] [INFO ] [main] [com.diy.sigmund.diycommon.spring.extensionpoint.CustomInstantiationAwareBeanPostProcessor] - bean = org.springframework.jdbc.datasource.DataSourceTransactionManager@16ca4532, beanName = transactionManager
 * <br>Line 2975: 2022-09-20 16:56:58.322 [diy-common] [172.25.96.1] [INFO ] [main] [com.diy.sigmund.diycommon.spring.extensionpoint.CustomInstantiationAwareBeanPostProcessor] - pvs = PropertyValues: length=0, bean = org.springframework.jdbc.datasource.DataSourceTransactionManager@16ca4532, beanName = transactionManager
 * <br>Line 2976: 2022-09-20 16:56:58.328 [diy-common] [172.25.96.1] [INFO ] [main] [com.diy.sigmund.diycommon.spring.extensionpoint.CustomInstantiationAwareBeanPostProcessor] - bean = org.springframework.jdbc.datasource.DataSourceTransactionManager@16ca4532, beanName = transactionManager
 * <br>Line 2977: 2022-09-20 16:56:58.336 [diy-common] [172.25.96.1] [INFO ] [main] [com.diy.sigmund.diycommon.spring.extensionpoint.CustomInstantiationAwareBeanPostProcessor] - bean = org.springframework.jdbc.datasource.DataSourceTransactionManager@16ca4532, beanName = transactionManager
 *
 * @author ylm-sigmund
 * @since 2022/9/20 16:48
 */
@Component
public class CustomInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

    private static final Logger log = LoggerFactory.getLogger(CustomInstantiationAwareBeanPostProcessor.class);

    private final String testPrintBeanName = new StringBuilder()
            .append(Character.toLowerCase(CustomBeanNameAware.class.getSimpleName().charAt(0)))
            .append(CustomBeanNameAware.class.getSimpleName().substring(1))
            .toString();

    /**
     * 实例化bean之前,相当于new这个bean之前
     */
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (Objects.equals(testPrintBeanName, beanName)) {
            log.info("CustomInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation:{}", beanName);
        }
        return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (Objects.equals(testPrintBeanName, beanName)) {
            log.info("CustomInstantiationAwareBeanPostProcessor.postProcessAfterInstantiation:{}", beanName);
        }
        return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if (Objects.equals(testPrintBeanName, beanName)) {
            log.info("CustomInstantiationAwareBeanPostProcessor.postProcessProperties:{}", beanName);
        }
        return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
    }

    /**
     * 初始化bean之前,相当于把bean注入到spring上下文之前
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (Objects.equals(testPrintBeanName, beanName)) {
            log.info("CustomInstantiationAwareBeanPostProcessor.postProcessBeforeInitialization:{}", beanName);
        }
        return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (Objects.equals(testPrintBeanName, beanName)) {
            log.info("CustomInstantiationAwareBeanPostProcessor.postProcessAfterInitialization:{}", beanName);
        }
        return InstantiationAwareBeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
